package com.szqhyxj.youxiangju.biz.plan.adapter;

import android.annotation.SuppressLint;
import android.support.annotation.NonNull;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.szqhyxj.youxiangju.R;
import com.szqhyxj.youxiangju.biz.plan.RepaymentModel;

import java.util.List;

import butterknife.BindColor;
import butterknife.BindView;
import butterknife.ButterKnife;

public class BilledAdapter extends RecyclerView.Adapter<BilledAdapter.IconViewHolder> {


    private List<RepaymentModel.RepaymentEntity> repaymentEntityList;

    private IconClickListener iconClickListener;

    public BilledAdapter(List<RepaymentModel.RepaymentEntity> repaymentEntityList, IconClickListener iconClickListener) {
        this.repaymentEntityList = repaymentEntityList;
        this.iconClickListener = iconClickListener;
    }


    @NonNull
    @Override
    public IconViewHolder onCreateViewHolder(@NonNull ViewGroup viewGroup, int i) {
        return new IconViewHolder(LayoutInflater.from(viewGroup.getContext()).inflate(R.layout.framelayout_billed_item, viewGroup, false));
    }

    @SuppressLint("ResourceAsColor")
    @Override
    public void onBindViewHolder(@NonNull IconViewHolder iconViewHolder, final int i) {
        final RepaymentModel.RepaymentEntity repaymentEntity = repaymentEntityList.get(i);
        Glide.with(iconViewHolder.itemView.getContext()).load(repaymentEntity.getCardLogo()).into(iconViewHolder.cardicon);
        iconViewHolder.cardname.setText(repaymentEntity.getBtName());
        String bankcardno = repaymentEntity.getBankCardNo();
        String bankcardnosub = bankcardno.substring(bankcardno.length() - 4, bankcardno.length());
        String cardno = "(" + bankcardnosub + ")";
        iconViewHolder.cardno.setText(cardno);
        String fixedamount = repaymentEntity.getFixedAmount();
        String str = fixedamount.substring(0, fixedamount.length() - 2);
        iconViewHolder.fixedamount.setText(str);
        iconViewHolder.intervaldate.setText(repaymentEntity.getDetDays());
        /*if (repaymentEntity.getBankStates() == 0) {
            iconViewHolder.certification.setText("进行报备");
            iconViewHolder.certification.setBackgroundResource(R.drawable.background_shangyibubtn);
            iconViewHolder.certification.setTextColor(iconViewHolder.biue);
        }*/
        if (repaymentEntity.getBankStates() == 1) {
            iconViewHolder.certification.setText("计划执行中");
            iconViewHolder.certification.setBackgroundResource(R.drawable.background_shangyibubtn);
            iconViewHolder.certification.setTextColor(iconViewHolder.biue);
        }
        if (repaymentEntity.getBankStates() == 2) {
            iconViewHolder.certification.setText("制定计划");
            iconViewHolder.certification.setBackgroundResource(R.drawable.background_feedbackbtn);
            iconViewHolder.certification.setTextColor(iconViewHolder.white);
        }
        if (repaymentEntity.getBankStates() == 4) {
            iconViewHolder.certification.setText("计划暂停中");
            iconViewHolder.certification.setBackgroundResource(R.drawable.background_shangyibubtn);
            iconViewHolder.certification.setTextColor(iconViewHolder.biue);
        }

        iconViewHolder.certification.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                iconClickListener.onClickItem(i, repaymentEntityList);
            }
        });
    }

    @Override
    public int getItemCount() {
        return repaymentEntityList.size();
    }

    static class IconViewHolder extends RecyclerView.ViewHolder {
        @BindView(R.id.cardicon)
        ImageView cardicon;
        @BindView(R.id.cardname)
        TextView cardname;
        @BindView(R.id.cardno)
        TextView cardno;
        @BindView(R.id.certification)
        TextView certification;
        @BindView(R.id.fixedamount)
        TextView fixedamount;
        @BindView(R.id.intervaldate)
        TextView intervaldate;
        @BindColor(R.color.invitationColor)
        int biue;
        @BindColor(R.color.white)
        int white;

        IconViewHolder(@NonNull View itemView) {
            super(itemView);
            ButterKnife.bind(this, itemView);
        }
    }

    public interface IconClickListener {
        void onClickItem(int i, List<RepaymentModel.RepaymentEntity> repaymentEntity);
    }
}